﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.PuntoDeVenta.ServiceReferenceApiAuth;
using CrmContacto.Contracts;
using CrmContacto.Contracts.Contracts;
using CrmContacto.Contracts.Contracts.Search;
using CrmContacto.Contracts.Contracts.Localization;
using Web.PuntoDeVenta.ServiceReferenceCrmLocalizationService;
using Web.Framework.Svc;
using CrmContacto.Services;

namespace Web.PuntoDeVenta.Code
{
    public class WebServiceHelper
    {
        public static bool Authenticate(int applicationId, int companyId, string userName, string password)
        {
            AuthApiClient client = new AuthApiClient();
            bool isAuthenticated = false;
            try
            {
                isAuthenticated = client.ValidateUser(applicationId, companyId, userName, password);
                client.Close();
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return isAuthenticated;
        }

        public static List<Company> GetCompanies(int applicationId)
        {
            AuthApiClient client = new AuthApiClient();
            List<Company> companies = new List<Company>();

            try
            {
                companies.AddRange(client.GetCompanies(applicationId));
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return companies;
        }

        public static List<AdUser> GetUsers(int applicationId, int companyId, string userName)
        {
            AuthApiClient client = new AuthApiClient();
            List<AdUser> usuarios = new List<AdUser>();
            try
            {
                usuarios.AddRange(client.GetUsers(applicationId, companyId, userName));
                client.Close();
            }
            catch (Exception ex)
            {

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return usuarios;
        }

        public static List<string> GetOperationsForUser(int applicationId, int companyId, string userName)
        {
            AuthApiClient client = new AuthApiClient();
            List<string> operations = new List<string>();

            try
            {
                operations.AddRange(client.GetOperationsForUser(applicationId, companyId, userName));
                client.Close();
            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return operations;
        }

        public static CustomerSearchResult SearchForCustomerByNameOrDocOrEmail(CustomerSearchParam searchParam)
        {                        
            PuntoDeVentaService client = new PuntoDeVentaService();
            CustomerSearchResult retVal;
            try
            {
                retVal = client.SearchForCustomerByNameOrDocOrEmail(searchParam);                
                
                return retVal;
            }
            catch (Exception ex)
            {               
                throw ex;
            }
        }

        public static List<MaestroBase> GetCampaniasActivas(CampaniasActivasSearch search)
        {
            PuntoDeVentaService client = new PuntoDeVentaService();
            try
            {
                var listMaestros = client.GetCampaniasActivas(search);
                
                return listMaestros;
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }      

        public static List<MaestroBase> GetTipoDocumento(int paisId)
        {
            PuntoDeVentaService client = new PuntoDeVentaService();
            try
            {
                var listMaestros = client.GetTipoDocumento(paisId);
                
                return listMaestros;
            }
            catch (Exception ex)
            {               
                throw ex;
            }
        }

        public static List<Pais> GetPaises()
        {
            PuntoDeVentaService client = new PuntoDeVentaService();
            try
            {
                var listMaestros = client.GetPaisesDto(new ServiceOptions(), new PaisSearch() { LoadProvincia = false });
                
                return listMaestros;
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }

        public static List<MaestroBase> GetProvincias(int paisId)
        {
            PuntoDeVentaService client = new PuntoDeVentaService();
            try
            {
                var listMaestros = client.GetProvincias(paisId);
                
                return listMaestros;
            }
            catch (Exception ex)
            {               
                throw ex;
            }
        }

        public static List<MaestroBase> GetCategoriasDeContacto()
        {
            PuntoDeVentaService client = new PuntoDeVentaService();
            try
            {
                var listMaestros = client.GetCategoriasDeContacto(new MaestroBaseSearch() { EmpresaId = BaseMasterPage.CompanyId });
                
                return listMaestros;
            }
            catch (Exception ex)
            {             
                throw ex;
            }
        }

        public static PtoVentaRegistroCliente SaveCustomer(PtoVentaRegistroCliente customer)
        {
            PuntoDeVentaService client = new PuntoDeVentaService();
            try
            {
                var ret = client.SaveCustomer(customer);
                
                return ret;
            }
            catch (Exception ex)
            {               
                throw ex;
            }
        }

        public static Dictionary<string, string> GetCompanyConfigurationKeyValues(int companyId)
        {
            Dictionary<string, string> companyConfigurationList = new Dictionary<string, string>(); 
            PuntoDeVentaService client = new PuntoDeVentaService();
            try
            {
                companyConfigurationList = client.GetCompanyConfigurationKeyValues(companyId);
                
                return companyConfigurationList;
            }
            catch (Exception ex)
            {               
                throw ex;
            }
        }

        public static List<CompanyConfiguration> GetCompanyConfigurations()
        {
            List<CompanyConfiguration> companyConfiguratinoList = new List<CompanyConfiguration>();
            PuntoDeVentaService client = new PuntoDeVentaService();
            try
            {
                companyConfiguratinoList.AddRange(client.GetCompanyConfigurations());
                
                return companyConfiguratinoList;
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }

        public static List<Lenguaje> ObtenerLenguajes()
        {
            LocalizationClient client = new LocalizationClient();
            try
            {
                var l = client.ObtenerLenguajes();
                
                return l;
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }

        public static Lenguaje ObtenerLenguajeById(string Id)
        {
            LocalizationClient client = new LocalizationClient();
            try
            {
                var l = client.ObtenerLenguajeById(Id);
                
                return l;
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }

        public static Lenguaje SaveLenguaje(Lenguaje lenguaje)
        {
            LocalizationClient client = new LocalizationClient();
            try
            {
                var l = client.SaveLenguaje(lenguaje);
                
                return l;
            }
            catch (Exception ex)
            {               
                throw ex;
            }
        }

        public static PalabraClave GetPalabraClaveLenguaje(string LenguajeId, string ClaveId)
        {
            LocalizationClient client = new LocalizationClient();
            try
            {
                var l = client.GetPalabraClaveLenguaje(LenguajeId, ClaveId);
                
                return l;
            }
            catch (Exception ex)
            {               
                throw ex;
            }
        }

        public static void EliminarLenguaje(string id)
        {
            LocalizationClient client = new LocalizationClient();
            try
            {
                client.EliminarLenguaje(id);
                                
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }

        public static PalabraClave SavePalabraClaveLenguaje(PalabraClave palabraClave)
        {
            LocalizationClient client = new LocalizationClient();
            try
            {
                var l = client.SavePalabraClaveLenguaje(palabraClave);
                
                return l;
            }
            catch (Exception ex)
            {               
                throw ex;
            }
        }

        public static CrmContacto.Contracts.Contracts.PuntoDeVenta GetPtoVentaCodeById(int puntoDeVentaId)
        {            
            PuntoDeVentaService client = new PuntoDeVentaService();
            try
            {
                var result = client.GetPtoVentaCodeById(puntoDeVentaId);
                
                return result;
            }
            catch (Exception ex)
            {               
                throw ex;
            }
        }

        public static PdvCantidadTotalesVentaResult GetCantidadTotalesVenta(PdvCantidadTotalesVentaSearch search)
        {
            PdvCantidadTotalesVentaResult result = new PdvCantidadTotalesVentaResult();
            PuntoDeVentaService client = new PuntoDeVentaService();
            try
            {
                result = client.GetCantidadTotalesVenta(search);
                
                return result;
            }
            catch (Exception ex)
            {                
                throw ex;
            }
        }

        public static Cliente GetCustomerById(ServiceOptions options, int customerId, ClienteByIdOptions clientOptions)
        {           
            PuntoDeVentaService client = new PuntoDeVentaService();
            try
            {
                var result = client.GetCustomerById(options, customerId, clientOptions);
                
                return result;
            }
            catch (Exception ex)
            {               
                throw ex;
            }
        }

        public static List<TipoMensajeBitacora> GetTipoMensajeBitacora(TipoMensajeBitacoraSearch search)
        {
            PuntoDeVentaService client = new PuntoDeVentaService();
            try
            {
                var result = client.GetTipoMensajeBitacora(search);
                
                return result;
            }
            catch (Exception ex)
            {               
                throw ex;
            }
        }

        public static ClienteBitacora SaveCustomerBitacora(ServiceOptions options, ClienteBitacora clienteBitacora)
        {
            PuntoDeVentaService client = new PuntoDeVentaService();
            try
            {
                var result = client.SaveCustomerBitacora(options, clienteBitacora);
                
                return result;
            }
            catch (Exception ex)
            {               
                throw ex;
            }
        }

        public static void ActivarEntidad(Web.Framework.Svc.ServiceOptions options, string entidad, int estado, int entidadId)
        {
            PuntoDeVentaService client = new PuntoDeVentaService();
            try
            {
                client.ActivarEntidad(options, entidad, estado, entidadId);
                                
            }
            catch (Exception ex)
            {               
                throw ex;
            }
        }

        public static List<CrmContacto.Contracts.Contracts.PuntoDeVenta> GetPuntoDeVentaDto(ServiceOptions options, PuntoDeVentaSearch search)
        {
            PuntoDeVentaService client = new PuntoDeVentaService();
            try
            {
                var result = client.GetPuntoDeVentaDto(options, search);
                
                return result;
            }
            catch (Exception ex)
            {               
                throw ex;
            }
        }
    }
}